//
//  WSPurchaseRequest.m
//  CharonTVProject
//
//  Created by karong on 2023/8/10.
//

#import "WSPurchaseRequest.h"
#import "WSGoods.h"

@implementation WSPurchaseRequest
+ (void)loadcharonTV_GoodsListWithGoods_type:(NSInteger)goods_type success:(charonTV_ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_CHARONTV_GOODS_LIST;
    NSDictionary * parameters = @{@"goods_type":@(goods_type)};
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [WSGoods mj_objectArrayWithKeyValuesArray:responseDict[@"items"]];
        NSMutableArray * mutArray = [[NSMutableArray alloc]initWithArray:array];
        for (WSGoods * goods in mutArray) {
            if (goods.is_first) {
                if ([mutArray indexOfObject:goods] > 0) {
                    [mutArray removeObject:goods];
                    [mutArray insertObject:goods atIndex:0];
                }
                break;
            }
        }
        success(mutArray);
    } failure:failed];
}

+ (void)ordercharonTV_ValidWithOrder_no:(NSString *)order_no receipt_data:(NSString *)receipt_data success:(charonTV_EmptyBlock)success failed:(Failure)failed {
    NSString * url = API_CHARONTV_ORDER_VALID;
    NSDictionary * parameters = @{@"order_no":WSEmptyStr(order_no),
                                  @"receipt_data":WSEmptyStr(receipt_data)};
    [self postAppleOrderWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        [appUserCenter getUserInfocharonTV_Request];
        success();
    } failure:failed];
}

+ (void)ordercharonTV_CreatWithGoods_id:(NSInteger )goods_id book_id:(NSInteger)book_id chapter_id:(NSInteger)chapter_id success:(charonTV_StringBlock)success failed:(Failure)failed {
    NSString * url = API_CHARONTV_ORDER_CREAT;
    NSMutableDictionary * parameters = [NSMutableDictionary new];
    parameters[@"goods_id"] = @(goods_id);
    parameters[@"pay_type"] = @(2);
    (chapter_id>0)?parameters[@"chapter_id"] = @(chapter_id):nil;
    (book_id>0)?parameters[@"book_id"] = @(book_id):nil;
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        success(responseDict[@"order_no"]);
    } failure:failed];
}
@end
